<?php
/**
 * 请求类
*/

namespace houze;

class Request
{
    /**
     * 获取请求信息
    */
    public static function request() {
        $Request = \houze\context\Request::get();
        if (!$Request) return false;
        return $Request;
    }

    /**
     * 获取请求头
    */
    public static function header() {
        $Request = \houze\context\Request::get();
        if (!$Request) return false;
        return $Request->header();
    }

    /**
     * 获取raw数据流
    */
    public static function raw() {
        $Request = \houze\context\Request::get();
        if (!$Request) return false;
        return $Request->rawBody();
    }

    /**
     * 获取get参数
    */
    public static function get($field=[]) {
        $Request = \houze\context\Request::get();
        if (!$Request) return false;
        return self::return_field($Request->get(), $field);
    }

    /**
     * 获取post参数
    */
    public static function post($field=[]) {
        $Request = \houze\context\Request::get();
        if (!$Request) return false;
        return self::return_field($Request->post(), $field);
    }

    /**
     * 获取get|post参数
    */
    public static function param($field=[]) {
        $Request = \houze\context\Request::get();
        if (!$Request) return false;

        if (!empty($Request->post)) return self::return_field($Request->post(), $field);
        return self::return_field($Request->get(), $field);
    }

    /**
     * 获取files参数
    */
    public static function file($field=[]) {
        $Request = \houze\context\Request::get();
        if (!$Request) return false;
        return self::return_field($Request->file(), $field);
    }
    
    /**
     * 判断是否GET请求
    */
    public static function is_get() {
        $Request = \houze\context\Request::get();
        if (!$Request) return false;
        if ($Request->method() == 'GET') return true;
        return false;
    }

    /**
     * 判断是否POST请求
    */
    public static function is_post() {
        $Request = \houze\context\Request::get();
        if (!$Request) return false;
        if ($Request->method() == 'POST') return true;
        return false;
    }

    /**
     * 判断是否AJAX请求
    */
    public static function is_ajax() {
        $Request = \houze\context\Request::get();
        if (!$Request) return false;
        if (isset($Request->header()['x-requested-with']) && $Request->header()['x-requested-with'] == 'XMLHttpRequest') {
            return true;
        }
        return false;
    }

    /**
     * 是否使用SSL
    */
    public static function is_ssl() {
        if (\houze\Config::get('server.ssl_cert_file') && \houze\Config::get('server.ssl_key_file')) return true;
        return false;
    }

    /**
     * 获取客户端真实IP
    */
    public static function ip() {
        $Response = \houze\context\Response::get();

        if (!empty($Response->getRemoteIp())) {
            return $Response->getRemoteIp();
        }
        return false;
    }

    /**
     * 获取当前域名
    */
    public static function domain() {
        $ret = 'http';
        if (self::is_ssl()) {
            $ret = 'https';
        }
        $Request = \houze\context\Request::get();
        return $ret.'://'.$Request->header('host');
    }

    /**
     * 获取当前请求路由
    */
    public static function route() {
        $Request = \houze\context\Request::get();
        if (!empty($Request->path())) {
            return $Request->path();
        }
        if (!empty($Request->uri())) {
            return $Request->uri();
        }
        return false;
    }

    /**
     * 获取完整URL
    */
    public static function url($status=false) {
        $ret = '';
        if ($status) {
            $ret = self::domain();
        }
        return $ret.self::route();
    }
    
    /**
     * 获取完整URL，带get参数
    */
    public static function baseUrl($status=false) {
        $ret = '';
        if ($status) {
            $ret = self::domain();
        }
        
        $Request = \houze\context\Request::get();
        $ret .= $Request->uri();
        
        return $ret;
    }

    /**
     * 返回值格式化
    */
    private static function return_field($param, $field) {
        if (!$param) return false;
        if (empty($field)) return $param;

        $list = [];
        foreach ($field as $key) {
            if (isset($param[$key])) $list[$key] = $param[$key];
        }

        return $list;
    }
}